/*
 * 单独的倒计时
 * 将天数转为小时数
 * 时分秒倒计时
 * 参数：时间戳,this
 * 页面要在data里添加 Areahours,Areaminutes,Areaseconds
 */
function Areacountdown(e, that) {
	return new Promise((rel, rej) => {
		let date = new Date();
		let time = date.getTime(); //当前时间
		if (e - time > 0) {
			var lag = (e - time);
			//计算出相差天数
			var days = Math.floor(lag / (24 * 3600 * 1000));
			//计算出小时数
			var leave1 = lag % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
			var hours = Math.floor(leave1 / (3600 * 1000));
			//计算相差分钟数
			var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
			var minutes = Math.floor(leave2 / (60 * 1000));
			//计算相差秒数
			var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
			var seconds = Math.round(leave3 / 1000);


			//只有一位数就在前面加0并转为字符串
			function part(val) {
				var lenval = (val + '').length;
				if (lenval >= 2) {
					return val;
				} else {
					return '0' + val;
				}
			}

			// var Areahours = (days * 24) + hours; //将天数转成时并加上原有的时
			var day = days + parseInt(hours / 24);
			that.day = part(day);
			that.Areahours = part(hours % 24); //时
			that.Areaminutes = part(minutes); //分
			that.Areaseconds = part(seconds); //秒

			//测试
			/* that.Areahours = part(0);
			that.Areaminutes = part(0);
			that.Areaseconds = part(5); */


			var setTime = setInterval(() => {
				var d = parseInt(that.day)
				var h = parseInt(that.Areahours);
				var m = parseInt(that.Areaminutes);
				var s = parseInt(that.Areaseconds);

				if (d > 0 || h > 0 || m > 0 || s > 0) {
					if (s > 0) {
						s -= 1;
						that.Areaseconds = part(s);
					} else {
						//分减一，秒加59
						if (m > 0) {
							m -= 1;
							that.Areaminutes = part(m);
							that.Areaseconds = 59;
						} else {
							//时减一，分加59，秒加59
							if (h > 0) {
								h -= 1;
								that.Areahours = part(h);
								that.Areaminutes = 59;
								that.Areaseconds = 59;
							} else {
								if (d > 0) {
									d -= 1;
									that.day = part(d);
									that.Areahours = 59;
									that.Areaminutes = 59;
									that.Areaseconds = 59;
								}
							}
						}
					}
				} else {
					rel(); //活动结束回调
					// console.log('倒计时结束！')
					clearInterval(setTime);
				}
			}, 1000)
		}
	})
}


export {
	Areacountdown,
}
